package com.example.administrator.mynews.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.example.administrator.mynews.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/20.
 */

public class DateWorkerFragment extends Fragment implements AdapterView.OnItemClickListener{
    private TextView textView;
    private GridView gridView;
    private List list;
    private StringBuffer stringBuffer=new StringBuffer("0");
    private int type;
    private final int TYPE_ADD=0;
    private final int TYPE_SUB=1;
    private final int TYPE_MUL=2;
    private final int TYPE_DIV=3;
    private int last;
    private int now;
    private int transfer;


    private String[] nums={"1","2","3","+","4","5","6","-","7","8","9","*","0","<--","=","/"};
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_dateworker,container,false);
        initView(view);
        return view;
    }

    private void initView(View view){
        textView= (TextView) view.findViewById(R.id.dateWorker_tv);
        gridView= (GridView) view.findViewById(R.id.dateWorker_gv);
        list=new ArrayList();
        for (int i=0;i<nums.length;i++){
            list.add(nums[i]);
        }
        Log.d("wangxu","shuzi=="+list.toString());

        ArrayAdapter adapter=new ArrayAdapter(getActivity(),R.layout.item_fragment_dateworker,
                R.id.item_dataWorker_btn,list);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(this);

    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //textView.setText("hahah");

        Log.d("qin","点击了item");
        switch (parent.getId()){
            case R.id.dateWorker_gv:
                switch (position){
                    case 0:
                        stringBuffer.append(position+1);
                        textView.setText(stringBuffer.toString());
                        break;
                    case 1:
                        stringBuffer.append(position+1);
                        textView.setText(stringBuffer.toString());
                        break;
                    case 2:
                        stringBuffer.append(position+1);
                        textView.setText(stringBuffer.toString());
                        break;
                    case 3:
                        type=TYPE_ADD;
                        last=Integer.parseInt(stringBuffer.toString());
                        stringBuffer=new StringBuffer("0");
                        break;
                    case 4:stringBuffer.append(position);
                        textView.setText(stringBuffer.toString());
                        break;
                    case 5:stringBuffer.append(position);
                        textView.setText(stringBuffer.toString());
                        break;
                    case 6:stringBuffer.append(position);
                        textView.setText(stringBuffer.toString());
                        break;
                    case 7:type=TYPE_SUB;
                        last=Integer.parseInt(stringBuffer.toString());
                        stringBuffer=new StringBuffer("0");break;
                    case 8:stringBuffer.append(position-1);
                        textView.setText(stringBuffer.toString());
                        break;
                    case 9:stringBuffer.append(position-1);
                        textView.setText(stringBuffer.toString());
                        break;
                    case 10:stringBuffer.append(position-1);
                        textView.setText(stringBuffer.toString());
                        break;
                    case 11:
                        break;
                    case 12:stringBuffer.append(position-12);
                        textView.setText(stringBuffer.toString());
                    case 13:
                        textView.setText("");
                        stringBuffer=new StringBuffer("");
                        break;
                    case 14:
                        getResult(type);
                        break;

                }
            break;
        }
    }
    public int getResult(int TYPE){
        int sum=0;
        if (TYPE==TYPE_ADD){
            now=Integer.parseInt(stringBuffer.toString());
            sum=last+now;
            Log.d("wangxu",sum+"sum");
        }
        if (TYPE==TYPE_SUB){
            now=Integer.parseInt(stringBuffer.toString());
            sum=last-now;
            Log.d("wangxu",sum+"sum");
        }
        textView.setText(sum+"");
        stringBuffer=new StringBuffer("");
        return 0;
    }

//    public void fourCounting(int TYPE){
//        if (TYPE==TYPE_ADD){
//            now=Integer.parseInt(stringBuffer.toString());
//            last=last+now;
//        }
//    }
}
